<?php

class ButtonColumn extends CButtonColumn {
	
	public $deleteButtonImageUrl = false;
	public $updateButtonImageUrl = false;
	public $viewButtonImageUrl = false;
	public $deleteButtonLabel = '删除';
	public $updateButtonLabel = '修改';
	public $updateButtonUrl='Yii::app()->controller->createUrl("edit",array("id"=>$data->primaryKey))';
	
	public $stateHandles = array();
	
	protected function initDefaultButtons() {
		return;
	}
	
	protected function renderDataCellContent($row, $data) {
		if($this->stateHandles) {
			$behaviors = $data->behaviors();
			$template = '';
			$enum = Yii::app()->enum;
			$buttons = array();
			foreach($this->stateHandles as $one=>$options) {
				$handleName = is_array($options) ? $one : $options;
				if(isset($behaviors[$handleName])) {
					$handle = $data->{$handleName};
					$ops = $handle->getOperations();
					foreach($ops as $opName) {
						if(isset($buttons[$opName])) continue;
						$buttons[$opName] = $options;
						$template .= '{'.$opName.'}';
					}
				}
			}
			ksort($buttons);
			foreach($buttons as $opName => $options) {
				if(is_array($options) && isset($options['url'])) {
					$url = $this->evaluateExpression($options['url'], array('op'=>$opName, 'data'=>$data));
					$url = "'{$url}'";
				} else {
					$url = 'Yii::app()->controller->createUrl("'.$opName.'", array("id"=>$data->id))';
				}
				$button=array(
					'label'=>$enum->lookup('dic', $opName, $opName),
					'url' => $url,
					'options' => array(
						'class' => $opName,
					),
				);
				$this->buttons[$opName] = $button;
			}
			
			$this->template = $template;
		} else {
			parent::initDefaultButtons();
		}
		
		parent::renderDataCellContent($row, $data);
	}
	
	public function renderButton($id,$button,$row,$data) {
		if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
  			return;
		$label=isset($button['label']) ? $button['label'] : $id;
		$url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
		$options=isset($button['options']) ? $button['options'] : array();
		if(!isset($options['title']))
			$options['title']=$label;
		$options['href'] = $url;
		$width = strlen($label) * 6 + 10;
		if (isset($options['style']))
			$options['style'] .= ';width:'.$width.'px';
		else 
			$options['style'] = 'width:'.$width.'px';
		if(!isset($options['class'])) $options['class'] = '';
		if($label == $this->deleteButtonLabel) {
			$options['class'] .= ' pn pnc';
		} else {
			$options['class'] .= ' pn';
		}
		echo CHtml::button($label, $options);
	}

}


?>